package fragment;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import base.BaseFragment;

import com.jhyj.shop.R;

/**
 *
 * 我的fragment ：将登录界面添加到fragment中
 * author:wbin
 * 2016.01.23
 */
public class My_Fragment extends BaseFragment {

    private WebView webview_my;
    private RelativeLayout rl_nonet;  //手机没网时的提示
    private ProgressBar pb_progressbar;
    private TextView tv_loading;
    private LinearLayout ll_progressbar;

    private static final String APP_CACHE_DIRNAME = "/webcache"; // web缓存目录
    private String url; // 网页url
    /**
     * 创建子视图对象
     * @return
     */
    @Override
    protected View initView() {
        View view = View.inflate(activity, R.layout.fragment_my, null);
        pb_progressbar = (ProgressBar)view.findViewById(R.id.pb_progressbar);
        tv_loading =(TextView)view.findViewById(R.id.tv_loading);
        ll_progressbar = (LinearLayout)view.findViewById(R.id.ll_progressbar);
        rl_nonet = (RelativeLayout)view.findViewById(R.id.rl_nonet);

        webview_my =(WebView)view.findViewById(R.id.webview_my);
        initWebView();
        ll_progressbar.setVisibility(view.VISIBLE);

        return view;
    }

    /**
     * 初始化webview
     */
    private void initWebView() {

        webview_my.getSettings().setJavaScriptEnabled(true);  //设置WebView支持javascript
        webview_my.getSettings().setUseWideViewPort(true);    //设置是当前html界面自适应屏幕
        webview_my.getSettings().setSupportZoom(true);
        webview_my.getSettings().setBuiltInZoomControls(true); //设置支持缩放
        webview_my.getSettings().setLoadWithOverviewMode(true);
        webview_my.getSettings().setDefaultTextEncodingName("UTF-8");
        webview_my.requestFocus();
        webview_my.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        // 建议缓存策略为，判断是否有网络，有的话，使用LOAD_DEFAULT,无网络时，使用LOAD_CACHE_ELSE_NETWORK
//        webview_my.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // 设置缓存模式
        // 开启DOM storage API 功能
        webview_my.getSettings().setDomStorageEnabled(true);
        // 开启database storage API功能
        webview_my.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = activity.getFilesDir().getAbsolutePath() + APP_CACHE_DIRNAME;
        Log.i("cachePath", cacheDirPath);
        // 设置数据库缓存路径
        webview_my.getSettings().setDatabasePath(cacheDirPath); // API 19 deprecated
        // 设置Application caches缓存目录
        webview_my.getSettings().setAppCachePath(cacheDirPath);
        // 开启Application Cache功能
        webview_my.getSettings().setAppCacheEnabled(true);
        webview_my.loadUrl("http://www.qpmall.com/msite/customer/index.html?tag=4&at=1");
        webview_my.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onLoadResource(WebView view, String url) {
                Log.i("TAG", "onLoadResource url=" + url);
                super.onLoadResource(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            // 页面开始时调用
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("TAG", "onPageStarted");
                super.onPageStarted(view, url, favicon);
            }

            // 页面加载完成调用
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ll_progressbar.setVisibility(view.INVISIBLE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Toast.makeText(activity, "亲，没网，请重新咻咻网络哦！", Toast.LENGTH_SHORT).show();
                rl_nonet.setVisibility(View.VISIBLE);
            }
        });
    }
}

